/*
 * This file is part of Euphrat.
 *
 * Copyright © 2012 Bart Verhoeven
 *
 * Euphrat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Euphrat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Euphrat. If not, see <http://www.gnu.org/licenses/>.
 */
package org.euphrat.client.view.widget;

import static com.google.gwt.user.client.DOM.getStyleAttribute;
import static com.google.gwt.user.client.DOM.setStyleAttribute;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Hyperlink;

/**
 * A hyperlink with hover effect. Applies a background color when the mouse
 * hovers over the link and restores its original background color when the
 * mouse hovers out of the link.
 *
 * @author Bart Verhoeven
 */
public class HoverLink extends Hyperlink {

  /** Name of the background color style attribute. */
  private static final String BACKGROUND_COLOR = "backgroundColor";

  /** Background color to apply when the mouse hovers over this link. */
  private final String mouseOverColor;
  /** Original background color of this link. */
  private String originalColor;

  /**
   * Creates a new hyperlink with hover effect.
   *
   * @param name name of this hyperlink
   * @param mouseOverColor background hover color to apply
   */
  public HoverLink(String name, String mouseOverColor) {
    super(name, name);
    this.mouseOverColor = mouseOverColor;
    sinkEvents(Event.MOUSEEVENTS);
    sinkEvents(Event.ONCLICK);
  }

  @Override
  public void onBrowserEvent(Event event) {
    switch (DOM.eventGetType(event)) {
      case Event.ONMOUSEOVER:
        onMouseOver();
        break;
      case Event.ONMOUSEOUT:
        onMouseOut();
        break;
      case Event.ONCLICK:
        onMouseClick();
        break;
    }

    super.onBrowserEvent(event);
  }

  /**
   * <p>Executed when the mouse hovers over this link.</p>
   *
   * <p>The default implementation records the original background color and
   * applies the background color to this link.</p>
   */
  protected void onMouseOver() {
    // Before applying the hover background color, record the original one.
    originalColor = getStyleAttribute(getElement(), BACKGROUND_COLOR);
    setStyleAttribute(getElement(), BACKGROUND_COLOR, mouseOverColor);
  }

  /**
   * <p>Executed when the mouse hovers out of this link.</p>
   *
   * <p>The default implementation restores the original background color of
   * this link.</p>
   */
  protected void onMouseOut() {
    // Restore the background color.
    setStyleAttribute(getElement(), BACKGROUND_COLOR, originalColor);
    originalColor = null;
  }

  /**
   * <p>Executed when when clicking on this link.</p>
   *
   * <p>The default implementation records the original background color.</p>
   */
  protected void onMouseClick() {
    // A selected link usually gets a different background color. Record it.
    originalColor = getStyleAttribute(getElement(), BACKGROUND_COLOR);
  }
}
